package com.itljm.cxrMarket.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itljm.cxrMarket.entity.Merchants;
import com.itljm.cxrMarket.entity.Products;
import com.itljm.cxrMarket.service.MerchantsService;
import com.itljm.cxrMarket.mapper.MerchantsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MerchantsServiceImpl extends ServiceImpl<MerchantsMapper, Merchants> implements MerchantsService{
    @Autowired
    private MerchantsMapper merchantsMapper;
    @Autowired
    private ProductsServiceImpl productsService;

    @Override
    public List<Merchants> selectMerchantByPage(Page<Products> page, Wrapper<Products> entityWrapper) {
        return merchantsMapper.selectMerchantByPage(page, entityWrapper);
    }

    @Override
    public Merchants selectMerchantById(Integer merchantId) {
        return merchantsMapper.selectById(merchantId);
    }

    @Override
    public List<Merchants> selectMerchantByName(String merchantName) {
        return merchantsMapper.selectMerchantByName(merchantName);
    }

    @Override
    public boolean saveOrUpdateMerchant(Merchants merchant) {
        Integer merchantId = merchant.getMerchantId();
        Integer merchantStatus = merchant.getMerchantStatus();
        if (merchantId != null && merchantStatus == 2) {
            // 如果商户状态为2(暂停营业)，将该商户所有商品下架
            productsService.updateProductStatusByMerchantId(merchantId, 0);
        }
        if (merchantId != null && merchantStatus == 1) {
            // 如果商户状态为1(营业中)，将该商户所有商品上架
            productsService.updateProductStatusByMerchantId(merchantId, 1);
        }
        return merchantsMapper.insertOrUpdate(merchant);
    }

    @Override
    public Integer selectMerchantIdByUserId(String userId) {
        return merchantsMapper.selectMerchantIdByUserId(userId);
    }

    @Override
    public Merchants selectMerchantByUserId(Long userId) {
        return merchantsMapper.selectMerchantByUserId(userId);
    }

    @Override
    public boolean checkMerchantStatusById(Integer merchantId, Integer checkStatus, String checkReason) {
        return merchantsMapper.checkMerchantStatusById(merchantId, checkStatus, checkReason)>0;
    }
}




